package jmlzz.example.com.imdemo.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessageBody;

import java.util.List;

import jmlzz.example.com.imdemo.R;
import jmlzz.example.com.imdemo.common.ThreadMannager;

/**
 * Created by robert.luozizhao on 2018/3/9.
 */

public class ChatActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mEtRecMessage;
    private EditText mEtSendMessage;
    private Button mBtnSend;
    private EditText mEtSendToWho;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        initView();
        initEvent();
    }

    EMMessageListener messageListener = new EMMessageListener() {
        @Override
        public void onMessageReceived(List<EMMessage> messages) {

            for (int i = 0; i < messages.size(); i++) {
                EMMessageBody body = messages.get(i).getBody();
                final String string = body.toString();
                ThreadMannager.postMainThread(new Runnable() {
                    @Override
                    public void run() {
                        String trim = mEtRecMessage.getText().toString().trim();

                        mEtRecMessage.setText(string+"\n"+trim);
                    }
                });
            }

        }

        @Override
        public void onCmdMessageReceived(List<EMMessage> messages) {

        }

        @Override
        public void onMessageRead(List<EMMessage> messages) {

        }

        @Override
        public void onMessageDelivered(List<EMMessage> messages) {

        }

        @Override
        public void onMessageChanged(EMMessage message, Object change) {

        }
    };


    private void initEvent() {
        EMClient.getInstance().chatManager().addMessageListener(messageListener);
    }

    private void initView() {
        mEtRecMessage = (EditText) findViewById(R.id.et_rec_message);
        mEtSendMessage = (EditText) findViewById(R.id.et_send_message);
        mBtnSend = (Button) findViewById(R.id.btn_send);
        mBtnSend.setOnClickListener(this);
        mEtSendToWho = (EditText) findViewById(R.id.et_send_to_who);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                submit();
                break;
        }
    }

    private void submit() {
        String message = mEtSendMessage.getText().toString().trim();
        String who = mEtSendToWho.getText().toString().trim();
        if (TextUtils.isEmpty(message)||TextUtils.isEmpty(who)) {
            Toast.makeText(this, "message或who不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        EMMessage mes = EMMessage.createTxtSendMessage(message,who);
        EMClient.getInstance().chatManager().sendMessage(mes);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EMClient.getInstance().chatManager().removeMessageListener(messageListener);
    }
}
